import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(),
      home: MyHome(),
    );
  }
}

class MyHome extends StatefulWidget {
  const MyHome({Key? key}) : super(key: key);

  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  List _list = List.generate(12, (index) => index);
  List<Widget> _widgetList = List.generate(
      12, (index) => TextButton(onPressed: () {}, child: Text(" ${index} ")));
  int _currentIndex = 0;

  _setIndex() {
    setState(() {
      // _currentIndex=index;
    });
  }

  @override
  void setState(VoidCallback fn) {
    super.setState(fn);
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
          height: 80,
          color: Colors.white,
          child: ListView.builder(
              itemCount: _list.length,
              scrollDirection: Axis.horizontal,
              itemBuilder: (BuildContext context, int index) {
                return TextButton(
                    onPressed: () {
                      setState(() {
                        _currentIndex = index;
                      });
                    },
                    child: Text("${index}"));
              })),
      Expanded(
        flex: 1,
        child: IndexedStack(
          index: _currentIndex,
          children: _list.map((e) {
            return PageA(e);
          }).toList(),
        ),
      )
    ]);
  }
}

class PageA extends StatefulWidget {
  final int index;

  const PageA(this.index, {Key? key}) : super(key: key);

  @override
  _PageAState createState() => _PageAState(index);
}

class _PageAState extends State<PageA> {
  final int index;

  _PageAState(this.index);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Text("$index"),
    );
  }
}
